/*
 * @Author: Creator
 * @Date: 2020-09-22 15:47:24
 * @LastEditors: Creator
 * @LastEditTime: 2020-10-09 15:03:25
 * @Description: file content
 */
#include <stdio.h>
//#include <stdbool.h>
#include "mytime.h"
#include <time.h>

int getToday(Date date)
{
    unsigned short y = date.year,m = date.month,d = date.day;
    int today = 0;
    if(m == 1 || m == 2)
    {
        m += 12;
        y--;
    }
    today = (int)(d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
    return today + 1;
}

bool isDateRight(Date date)
{
    bool leap = false;
    unsigned short months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    if((date.year % 100) ? (!(date.year % 4)):(!(date.year % 400)))
        months[2] = 29;
    if(date.month && date.month < 13 && date.day && date.day <= months[date.month])
        return true;
    else
        return false;
}

int cmpDate(Date s,Date t)
{
    int result = 0;
    if(s.year == t.year)
    {
        if(s.month == t.month)
        {
            if(s.day == t.day)
            {
                if(s.hour == t.hour)
                {
                    if(s.min == t.min)
                        return 0;
                    else
                        result = s.min - t.min;
                }
                else
                    result = s.hour - t.hour;
            }
            else
                result = s.day - t.day;
            
        }
        else
            result = s.month - t.month;
        
    }
    else
        result = s.year - t.year;
    return result;
    
}

Date getNowtime()
{
    Date date;
    time_t rawtime;
    struct tm * timeinfo;
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    date.year = timeinfo->tm_year + LinuxTimeBegin;
    date.month = timeinfo->tm_mon + 1;
    date.day = timeinfo->tm_mday;
    date.hour = timeinfo->tm_hour;
    date.min = timeinfo->tm_min;
    return date;
}

void displayTime(Date date)
{
    char today[8][4] = {"0","一","二","三","四","五","六","日"};        // 注意汉字的存储方式 占 二位 char
    printf("%d年%d月%d日 星期%s %d:%d\n",date.year,date.month,date.day,today[getToday(date)],date.hour,date.min);
    // 汉字占两位
}

Date readTime(FILE *fp)
{
    Date temp;
    fscanf(fp,"%d %d %d %d %d",
        &(temp.year),
        &(temp.month),
        &(temp.day),
        &(temp.hour),
        &(temp.min));
    return temp;
}

bool writeTime(FILE *fp, Date *date)
{
    int temp = fprintf(fp,"%d %d %d %d %d ",
        date->year,
        date->month,
        date->day,
        date->hour,
        date->min);
    
    return 1;
    
}